package com.zw.yzk.kabu.data.repository;

import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.data.cache.store.UserStore;
import com.zw.yzk.kabu.data.constant.Constant;
import com.zw.yzk.kabu.data.network.ServiceGenerator;
import com.zw.yzk.kabu.data.network.entity.ModifyPasswordEntity;
import com.zw.yzk.kabu.data.network.service.ModifyPasswordService;
import com.zw.yzk.kabu.data.network.task.ModifyPasswordTask;

import java.net.URLEncoder;

import javax.inject.Inject;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by zhanwei on 2016/12/24.
 */

public class ModifyPasswordRepository extends CommonRepository {

    @Inject
    UserStore userStore;

    @Inject
    public ModifyPasswordRepository() {

    }

    @Override
    protected Observable connectServer(Task task) {
        ModifyPasswordService loginService = ServiceGenerator.createService(ModifyPasswordService.class);
        ModifyPasswordTask modifyPasswordTask = (ModifyPasswordTask) task;

        return loginService.modifyPassword(modifyPasswordTask);
    }

    @Override
    protected Func1<ModifyPasswordEntity, Observable> getServerData() {
        return new Func1<ModifyPasswordEntity, Observable>() {
            @Override
            public Observable call(ModifyPasswordEntity entity) {
                if (entity.errCode == Constant.RESULT_OK) {
                    userStore.updateLoginUserToken(entity.result.accessToken);
                    return Observable.just(true);
                } else {
                    return Observable.just(false);
                }
            }
        };
    }

}
